﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunny.UI;
using System.Runtime.InteropServices;
using Newtonsoft.Json.Linq;
using SqlSugar.Extensions;

namespace CoreShop.App
{
    public partial class UIKeyBoard : UIUserControl
    {
        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        public static bool GetState(VirtualKeys Key)
        {
            return (GetKeyState((int)Key) == 1);
        }

        const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        const uint KEYEVENTF_KEYUP = 0x2;

        public static void SetState(VirtualKeys Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }

        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90, //数字锁定键
            VK_SCROLL = 0x91,  //滚动锁定
            VK_CAPITAL = 0x14, //大小写锁定
            VK_A = 62
        }

        //private UIEdit _input;
        public UIKeyBoard()
        {
            InitializeComponent();
        }
        
        //public UIEdit InputControl
        //{
        //    get
        //    {
        //        return _input;
        //    }
        //    set
        //    {
        //        _input = value;
        //    }
        //}

        private void btnOK_Click(object sender, EventArgs e)
        {
            Form Frm = this.FindForm();
            if (Frm is BaseForm)
            {
                (Frm as BaseForm).DoOK();
            }
        }

        private void lbBack_Click(object sender, EventArgs e)
        {
            try
            {
                //_input.Focus();
                SendKeys.Send("\b");
            }
            catch { }
        }

        public event EventHandler OnValueChanged;
        private void b1_Click(object sender, EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            int tag = btn.Tag.ToString().ToInt();
            if (btn.Name.Length == 1)
            {
                //DoValueChanged(this, tag - 32);
                OnValueChanged?.Invoke(btn, EventArgs.Empty);
            }
            else
            {
                OnValueChanged?.Invoke(btn, EventArgs.Empty);
            }

        }
    }
}
